package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.frontVo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontVo.CourseWebVo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-09
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    //注入课程描述
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;
    //注入小节
    @Autowired
    private EduVideoService videoService;
    //注入章节
    @Autowired
    private EduChapterService   chapterService;


    //添加课程基本信息  向两张表加数据  课程表 课程描述表
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        // 1. 向课程表添加数据 课程基本信息
        /**
         *  将courseInfoVo 对象转换为 eduCourse对象
         */
        EduCourse eduCourse =new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);

        int insert = baseMapper.insert(eduCourse);

        if(insert==0){
            //添加失败
            throw new GuliException(20001,"添加课程失败");
        }
        //获取添加课程成功之后的课程ID 做关联
        String cid =eduCourse.getId();

        //  2.向课程描述表添加课程简介
        EduCourseDescription courseDescription = new EduCourseDescription();

        //少数值就可以不采用 BeanUtils.copyProperties(courseInfoVo,eduCourse);
        courseDescription.setDescription(courseInfoVo.getDescription());

        //手动设置id就是描述id    以 课程id为准
        courseDescription.setId(cid);

        courseDescriptionService.save(courseDescription);

        return cid; //返回课程id
    }
    //根据课程id 查询课程的基本信息  课程表  课程描述表
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //1.课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);

        CourseInfoVo courseInfoVo=new CourseInfoVo();

        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        //2.描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);//用的同一个id

        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;

    }
    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {

        EduCourse eduCourse =new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);

        //修改课程表
        int update = baseMapper.updateById(eduCourse);
        if(update==0){
            throw new GuliException(20001,"修改课程信息失败！");
        }
        EduCourseDescription description= new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        //修改描述表
        courseDescriptionService.updateById(description);
    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {

        //调自己的mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    /**
     * 删除课程  课程表 课程描述表 章节表 小结表
     * @param courseId
     */
    @Override
    public void removeCourse(String courseId) {

        //1. 根据课程id 删除小结
        videoService.removeVideoByCourseId(courseId);

        //2.根据课程id 删除章节
        chapterService.removeChapterBycourseId(courseId);
        //3.根据课程id 删除描述
         courseDescriptionService.removeById(courseId);
        //4.根据课程id 删除本身
        int result = baseMapper.deleteById(courseId);

        if(result==0){
            throw  new GuliException(20001,"删除失败");
        }

    }
    //条件查询带分页查询课程
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {

        //2 根据讲师id查询所讲课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空拼接
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) {  //一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())) {       //二级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) {    //关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) {  //最新
            wrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {      //价格
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam,wrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();//下一页
        boolean hasPrevious = pageParam.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //map返回
        return map;
    }
    //根据课程id，编写sql语句查询课程信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
